#!/bin/bash
#函数变量
docker_images(){
  local bashpath="$(cd `dirname $0`;pwd)"
  if [ -e ${bashpath}/file/images_prometheus.tar.gz ];then
      cd ${bashpath}/file/
      tar -xvf images_prometheus.tar.gz
      cd images_prometheus
        for ii in `ls`
           do
              docker load -i $ii
        done
    else
docker pull prom/node-exporter
docker pull prom/mysqld-exporter
docker pull google/cadvisor
docker pull prom/prometheus
docker pull grafana/grafana
fi
echo -e "\033[032m========================= check ==============================\033[0m"
docker_images_old=`docker images --format {{.Repository}}:{{.Tag}}`
for i in ${docker_images_old}
do
  echo -e "\033[032m $i \033[0m"
done
}

check(){
a=`grep "disabled$" /etc/selinux/config`
b=`systemctl status firewalld|awk '/Active/{print $2}'`
if [ $a == SELINUX=disabled ];then
   echo -e  "\033[032m selinux已成功禁用 \033[0m"
  else
    echo -e  "\033[031m selinux 未禁用 \033[0m"
    help_me
    exit 0
fi
if [ $b == inactive ];then
  echo -e "\033[032m firewalld已成功禁用 \033[0m"
else
  echo -e  "\033[031m firewalld未禁用 \033[0m"
  help_me
  fi
}
network(){
  ping -c 3 www.baidu.com &>/dev/null
if [ $? -eq 0 ];then
  echo -e "\033[32m 网络连接正常 \033[0m"
  else
    echo -e "\033[31m 网络连接失败 \033[0m"
    echo -e "\033[31m 请检查网络是否正常 \033[0m"
    exit 0
fi
}
ipaddr=`ifconfig -a |grep -v docker| grep inet | grep -v inet6 | grep -v 127.0.0.1 | awk '{print $2}' |grep -v 172`
docker_install(){
  local bashpath="$(cd `dirname $0`;pwd)"
if [ -e ${bashpath}/file/docker-ce.tar.gz ];then
    tar -xvf ${bashpath}/file/docker-ce.tar.gz
    if [ -f ${bashpath}/file/dockerdocker-ce.repo ];then
        cp docker-ce.repo /etc/yum.repos.d/docker-ce.repo
    else
        echo -e "\033[31m 【`hostname` ERROR not find ${bashpath}/file/docker/docker-ce.repo 】"
        exit 0
    fi
    yum -y install docker-ce
    systemctl enable docker --now
else
yum install -y yum-utils net-tools bash-completion vim
yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
yum -y install docker-ce
systemctl enable  docker
systemctl start docker
fi
#开启路由转发
cat >>/etc/sysctl.conf<<EOF
net.ipv4.ip_forward = 1
EOF
sysctl -p
}
#检查docker是否安装成功
reload(){
  systemctl  daemon-reload
  systemctl restart docker
}

#启用镜像加速
registory(){
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://geuj9lut.mirror.aliyuncs.com"]
}
EOF
}

#验证docker服务是否正常启用
check_docker(){
local docker_num_1=`ifconfig  docker0`  docer_version=`docker version` docker_num_2=`ifconfig  |grep docker0|awk '{print $1}'|tr -d :`
if [ ${docker_num_2} == docker0 ];then
  echo -e "\033[033 ${docker_num_1} \033[0m"
  echo -e "\033[033 ${docer_version} \033[0m"
else
  echo -e "\033[032 docker install fail \033[0m"
fi
}

#修改service文件
# 在 ExecStart 下面添加

basepath=`pwd`
#启动镜像
docker_run(){
  #启动node-exporter
docker run -d --name node-exporter --restart=always -p 9100:9100 -v "/proc:/host/proc:ro" -v "/sys:/host/sys:ro" -v "/:/rootfs:ro" prom/node-exporter
#启动mysqld-exporter
docker run -d --name mysqld_exporter --restart=always -p 9104:9104 -e DATA_SOURCE_NAME="root:123456@(172.17.0.2:3306)/" prom/mysqld-exporter
#启动cadvisor
docker run -v /:/rootfs:ro -v /var/run:/var/run:rw -v /sys:/sys:ro -v /var/lib/docker/:/var/lib/docker:ro -v /dev/disk/:/dev/disk:ro -p 8080:8080 -d --name=cadvisor --restart=always google/cadvisor:latest
#启动prometheus
docker run  -d --name prometheus --restart=always -p 9090:9090 -v ${basepath}/../prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
docker run -d --name grafana --restart=always -p 3000:3000 --name=grafana -v /opt/grafana-storage:/opt/grafana grafana/grafana

}
check_image()
{
  local image_name=$1
  INFO "check image [${image_name}]"
  local ret=`docker images --format {{.Repository}}:{{.Tag}} | grep ^${image_name}$ | wc -l`
  local result=0
  if [ ${ret} -eq 0 ]; then
    INFO "not found image"
    result=1
  else 
    INFO "found image, image exits"
    result=0
  fi
  return ${result}
}


docker_install_num(){
  #加载镜像加速器
registory

#重新加载docker服务
reload
#验证docker服务是否正常启用
check_docker
#下载镜像
docker_images
#docker启动容器
docker_run
#启动完成后，web检查功能，输出URL
log
}

#启动容器
container_run(){
    local basepath=`pwd`
  #启动node-exporter
docker run -d --name node-exporter --restart=always -p 9100:9100 -v "/proc:/host/proc:ro" -v "/sys:/host/sys:ro" -v "/:/rootfs:ro" prom/node-exporter
#启动mysqld-exporter
docker run -d --name mysqld_exporter --restart=always -p 9104:9104 -e DATA_SOURCE_NAME="root:123456@(172.17.0.2:3306)/" prom/mysqld-exporter
#启动cadvisor
docker run -v /:/rootfs:ro -v /var/run:/var/run:rw -v /sys:/sys:ro -v /var/lib/docker/:/var/lib/docker:ro -v /dev/disk/:/dev/disk:ro -p 8080:8080 -d --name=cadvisor --restart=always google/cadvisor:latest
#启动prometheus
docker run  -d --name prometheus --restart=always -p 9090:9090 -v ${basepath}/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
docker run -d --name grafana --restart=always -p 3000:3000 --name=grafana -v /opt/grafana-storage:/opt/grafana grafana/grafana

}
#停止所有正在使用的容器服务
container_stop(){
  local container_name=`docker ps -a  --format  {{.Names}}`
for i in ${container_name}
  do
    docker stop $i
done
}
#删除所有正在使用的容器
container_rm(){
  local container_name=`docker ps -a --format  {{.Names}}`
  for names in ${container_name}
  do
    docker rm $names
  done
}
#启动所有容器，包括已停止，但未删除的容器
container_start(){
  local container_name=`docker ps -a --format  {{.Names}}`
  for names in ${container_name}
  do
    docker start $names
  done
}
#启动单个容器
container_all(){
   local env=$1
   case $env in
    'start')
        docker start $2 ;;
    'stop')
        docker stop $2 ;;
    'restart')
        docker restart $2;;
  esac

}

change_service(){
[ -f /lib/systemd/system/docker.service ] && sed -i '/ExecStart/aExecStartPost=/sbin/iptables -P FORWARD ACCEPT' /lib/systemd/system/docker.service \
|| echo "/lib/systemd/system/docker.service不存在";exit 0
docker_install_num
}
log(){
  local ip_add=`ifconfig -a | grep inet | grep -v inet6 | grep -v 127.0.0.1 | awk '{print $2}' |tail -1`
  echo -e "\033[032m http://${ip_add}:9100/metrics \033[0m"
  echo -e "\033[032m http://${ip_add}:9104/metrics \033[0m"
  echo -e "\033[032m http://${ip_add}:8080/metrics \033[0m"
  echo -e "\033[032m http://${ip_add}:9090/graph \033[0m"
  echo -e "\033[032m http://${ip_add}:9090/targets \033[0m"
  echo -e "\033[032m http://${ip_add}:3000/ \033[0m"
}
log_user(){
  echo -e "\033[031m执行脚本的命令：\033[0m"
  echo -e "\033[031m sh run.sh install|start|stop|restart \033[0m"
  echo -e "\033[031m sh run.sh start|stop|restart 容器名 \033[0m"
  echo -e "\033[031m 联网情况下，且网络状态不好的情况下，先下载README.md文件中\n 离线安装包的下载地址，下载完成后放到指定的目录\033[0m"
  echo -e "\033[031m 第一步，先检查服务器环境 \033[0m"
  echo -e "\033[031m 第二步，执行本目录下的sh check.sh脚本 \033[0m"
  echo -e "\033[031m 第三步，确认当前服务器是否连接外网，如若未连接，请到有外网的环境中使用 \033[0m"
  echo -e "\033[031m 第四步，确认无误后，执行上述命令\033[0m"
}
user(){
  if [[ $# -ne 1 ]] || [[ "$1" != "install" && "$1" != ""   ]] || \
     [[ "$2" != "start"  ]];then
    log "user: sh run.sh install|start"
    exit 0
  fi
}
help_me(){
  echo -e "\033[032m \033[0m"
  echo -e "\033[032m 执行下面的命令，可以设置seLinux为disabled，执行后重启生效 \033[0m"
  echo -e "\033[031m sed -i 's#SELINUX=enforcing#SELINUX=disabled#' /etc/selinux/config \033[0m"
  echo -e "\033[032m 执行下面的命令，可以设置firewalld为disabled \033[0m"
  echo -e "\033[031m systemctl stop firewalld;systemctl disable firewalld\033[0m"
}
